﻿// --------------------------------------------------------------------------------------
// <copyright file="FormAuthentication.cs" company="NGroup">
// Copyright (c) NGroup. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------
// <history>
//     <change who="Bui Thanh Nam" date="2013.12.21">Create</change>
// </history>
// --------------------------------------------------------------------------------------

namespace NGroup.VTL.Core.Authentication
{
    using System;
    using System.Web;
    using System.Web.Security;

    public class FormAuthentication : IAuthentication
    {
        public void Signin(string userName, bool persistent)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentException("userName cannot be null or empty.", "userName");
            }

            SetAuthCookie(HttpContext.Current, new FormsAuthenticationTicket(userName, persistent,
                Convert.ToInt32(FormsAuthentication.Timeout.TotalMinutes)));
        }

        public void Signout()
        {
            if (HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session.Abandon();
                HttpContext.Current.Session.RemoveAll();
            }

            FormsAuthentication.SignOut();
        }

        /// <summary>
        /// Adds the encrypted <see cref="FormsAuthenticationTicket"/> to the response stream with an expiration of 20 minutes from <see cref="DateTime.Now"/>.
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="authenticationTicket"></param>
        public void SetAuthCookie(HttpContextBase httpContext, FormsAuthenticationTicket authenticationTicket)
        {
            var encryptedTicket = FormsAuthentication.Encrypt(authenticationTicket);
            httpContext.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { Expires = CalculateCookieExpirationDate() });
        }

        /// <summary>
        /// Adds the encrypted <see cref="FormsAuthenticationTicket"/> to the response stream with an expiration of 20 minutes from <see cref="DateTime.Now"/>.
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="authenticationTicket"></param>
        public void SetAuthCookie(HttpContext httpContext, FormsAuthenticationTicket authenticationTicket)
        {
            FormsAuthentication.SetAuthCookie(authenticationTicket.Name, false);
            var encryptedTicket = FormsAuthentication.Encrypt(authenticationTicket);
            httpContext.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { Expires = CalculateCookieExpirationDate() });
        }

        public FormsAuthenticationTicket Decrypt(string encryptedTicket)
        {
            return FormsAuthentication.Decrypt(encryptedTicket);
        }

        private static DateTime CalculateCookieExpirationDate()
        {
            return DateTime.Now.Add(FormsAuthentication.Timeout);
        }
    }
}
